/**
 * @desc: 1.存在重复元素
 */
function containsDuplicate(nums) {
  let newNums = [...new Set(nums)];
  return nums.length === newNums.length ? true : false;
}
// console.log(containsDuplicate([1, 1, 2, 3, 4, 5]));
// console.log(containsDuplicate([1, 2, 3, 4, 5]));

/**
 * @desc: 2.有效的字母异位词
 */
function isAnagram(s, t) {
  let a = s.split("").sort().join("");
  let b = t.split("").sort().join("");
  return a === b ? true : false;
}
// console.log(isAnagram("anagram", "nagaram"));
// console.log(isAnagram("rat", "car"));

/**
 * @desc: 3.两数之和
 */
function twoSum(nums, target) {
  let a = new Map();
  for (let i in nums) {
    let b = target - nums[i];
    if (a.has(b)) {
      return [Number(a.get(b)), Number(i)];
    }
    a.set(nums[i], i);
  }
  return [];
}
// console.log(twoSum([2, 7, 11, 15], 9));
// console.log(twoSum([3, 2, 4], 6));
// console.log(twoSum([3, 3], 6));

/**
 * @desc: 4.字母词以为分组
 */
function groupAnagrams(strs) {
  let map = new Map();
  for (let i of strs) {
    let key = i.split("").sort().join("");
    let list = map.get(key) ? map.get(key) : [];
    list.push(i);
    map.set(key, list);
  }
  return Array.from(map.values());
}
// console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
// console.log(groupAnagrams([""]));
// console.log(groupAnagrams(["a"]));

/**
 * @desc: 5.前 k 个高频元素
 */
function topKFrequent(nums, k) {
  let arr = [];
  let obj = {};
  for (let num of nums) {
    obj[num] = obj[num] ? obj[num] + 1 : 1;
  }
  for (let key in obj) {
    if (obj[key] >= k) {
      arr.push(parseInt(key));
    }
  }
  return arr;
}
// console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2));
// console.log(topKFrequent([1], 1));
// console.log(topKFrequent([3, 4, 3, 2, 1, 3, 3], 3));

/**
 * @desc: 6.除自身以外数组的乘积
 */
function productExceptSelf(nums) {}
